# -*- coding: utf-8 -*-
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db

from emof import core as EMOF
from emof import dumper
from emof import loader

from model import Model

class LoadOperationData(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        a_id = self.request.get("id")
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        self.response.headers['Content-Type'] = 'application/xml'
        xml = "<r></r>"
        
        a = rep.findOperation(a_id)
        template_values = {'id': a_id,
                           'p': a}
        path = os.path.join(os.getcwd(), 'xml/Operation.xml')
        xml = template.render(path, template_values)
                
        self.response.out.write(xml)
                
class SaveOperationData(webapp.RequestHandler):
    def post(self):
        m_id = self.request.get("m_id")
        a_id = self.request.get("id")
        name = self.request.get("name")
        type = self.request.get("type")
        if self.request.get("isOrdered") == '1':
            isOrdered = True
        else:
            isOrdered = False
        if self.request.get("isUnique") == '1':
            isUnique = True
        else:
            isUnique = False
        lower = self.request.get("lower")
        upper = self.request.get("upper")
        raisedExceptions = self.request.get_all("raisedException")
        
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        
        o = rep.findOperation(a_id)
        
        if type:
            t = rep.findType(type)
            if t:
                o.type = t
        else:
            o.type = None
        
        o.name = name
        o.isOrdered = isOrdered
        o.isUnique = isUnique
        o.lower = lower
        o.upper = upper
        raisedException = list()
        
        if raisedExceptions:
            for re in raisedExceptions:
                t = rep.findType(re)
                if t:
                    raisedException.append(t)
        
        o.raisedException = raisedException
                    
        model.model_XMI = dumper.Dumper().dump(rep)
        model.put()
                
        self.response.out.write("OK")
        
class CreateOperationData(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        
        if self.request.get("p_id") != "":
            p_id = self.request.get("p_id")
        else:
            p_id = '-1'
            
        model = Model.get(m_id)

        rep = loader.Loader().load(model.model_XMI)
        fact = EMOF.Factory(rep)       
        o = fact.create_Operation()
        o.name = 'New Operation'
        o.isOrdered = False
        o.isUnique = True
        o.lower = 1
        o.upper = 1

        clss = rep.findClass(p_id)

        if clss:
            clss.ownedOperation.append(o)
            o.class_ = clss
            
        model.model_XMI = dumper.Dumper().dump(rep)
        model.put()
                    
        self.response.out.write(o.id + '$$$$' + o.name)
